// pages/coupons/goods.js
import api from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rcId:'',
    itemList:[],
    item:{
      "itemId":"6",
      "itemName": "小草莓",
      "categoryId": "1",
      "rcId": "1",
      "price": 1.0,
      "supplier": 1,
      "description": "1",
      "image1": "1",
      "image2": "1"
    }, 
    num:[2,1,1,1,1,1,1,1,1,1,1,1,1,1],//购买数量
    option1: [
      { text: '全部商品', value: 0 },
      { text: '新款商品', value: 1 },
      { text: '活动商品', value: 2 },
    ],
    option2: [
      { text: '默认排序', value: 'a' },
      { text: '好评排序', value: 'b' },
      { text: '销量排序', value: 'c' },
    ],
    option3: [
      { text: '默认筛选', value: 'a' },
      { text: '好评排序', value: 'b' },
      { text: '销量排序', value: 'c' },
    ],
    value1: 0,
    value2: 'a',
    value3: 'a',
    value4: 'a',
    value5: 'a',
    value6: 'a',
    value7: 'a',
    value8: 'a',
    value11: 0,
    //修改商品的动作面板
    show: false,
    actions: [],
 

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.setData({rcId:e.rcId})
    const url = '/items/RC/'+e.rcId
    api.get(url,{
      data:''
    }).then(res=>{
      console.log(res)
      this.setData({itemList:res.data})
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  pay:function(){
    wx.navigateTo({
      url: '../../pages/to-pay-order/index',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(e) {
    this.setData({
      value: e.detail,
    });
  },
  onSearch() {
    Toast('搜索' + this.data.value);
  },
  onClick() {
    Toast('搜索' + this.data.value);
  },
  onConfirm() {
    this.selectComponent('#item').toggle();
  },

  onSwitch1Change({ detail }) {
    this.setData({ switch1: detail });
  },

  onSwitch2Change({ detail }) {
    this.setData({ switch2: detail });
  },

  onNumAdd(e){
    console.log("点击了增加按钮")
    var index=e.currentTarget.dataset.index;
   
    console.log(index)
    console.log(this.data.num[index])
    var curnum=this.data.num[index];
    curnum=curnum+1
    
    var cnum='num['+index+']';
   this.setData({
     [cnum]:curnum
   })
  },

  onActionSheetShow(e) {
    console.log('e')
    console.log(e)
    var item = e.currentTarget.dataset
    this.setData({
      item:{
        "itemId":item.itemid,
        "itemName": item.itemname,
        "categoryId": item.categoryid,
        "rcId": item.rcid,
        "price": item.price,
        "supplier": 1,
        "description": item.description,
        "image1": item.image1,
        "image2": item.image1
      }
    })
    this.setData({ show: true });
  },
  onClose() {
    console.log("false")
    this.setData({ show: false });
  },
  onSubmit() {
    console.log(this.data.item)
    var url = '/items/'+this.data.item.itemId+'/RC/'+this.data.item.rcId
    api.put(url,this.data.item)
      .then(res=>{
        console.log("res")
        console.log(res)
        //onload
        url = '/items/RC/'+this.data.rcId
        api.get(url,{
          data:''
        }).then(res=>{
          console.log(res)
          this.setData({itemList:res.data})
        })
      })
    this.setData({show:false})
    
  },
  onCancel(){
    console.log("cancel")
    this.setData({show:false})
  },
  onInput(e){
    console.log(e)
    console.log(e.currentTarget.id)
    this.setData({[e.currentTarget.id]: e.detail})
    console.log(this.data.item)
  },
  //滑动删除
  onOpen(event) {
    console.log(event.detail)
    const { position, instance } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        console.log('delete')
        break;
    }
  },
  onDelete(e){
    
    console.log(e)
    var data = e.currentTarget.dataset
    var url = '/items/'+data.itemid+'/RC/'+data.rcid
    api.remove(url,{
      data:''
    }).then(res=>{
      console.log(res)
      //onload
      url = '/items/RC/'+this.data.rcId
      api.get(url,{
        data:''
      }).then(res=>{
        console.log(res)
        this.setData({itemList:res.data})
      })
    })
  }
})